package SMSGUI;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadExcel {

	public static HashMap<Integer, String[]> read(String inputFile, int column,
			int readcolumn, int dataType) throws IOException {
		HashMap<Integer, String[]> columnData = new HashMap<Integer, String[]>();
		String[] dataSentiment;
		File inputWorkbook = new File(inputFile);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);
			// Loop over first 10 column and lines
			for (int i = 1; i < sheet.getRows(); i++) {
				if (sheet.getCell(3, i).getContents().equals("" + dataType)) // if
																				// (3)
																				// we
																				// only
																				// take
																				// tweets!!
				{
					dataSentiment = new String[2];
					Cell celldata = sheet.getCell(column, i);
					Cell sentiment = sheet.getCell(readcolumn, i);
					// if(Integer.parseInt(sentiment.getContents()) < 0){
					dataSentiment[0] = celldata.getContents();
					dataSentiment[1] = sentiment.getContents();
					columnData.put(i, dataSentiment);
					// }
				}
			}

		} catch (BiffException e) {
			e.printStackTrace();
		}
		return columnData;
	}

	public static HashMap<Integer, String[]> read(String inputFile, int column,
			int readcolumn) throws IOException {
		HashMap<Integer, String[]> columnData = new HashMap<Integer, String[]>();
		String[] dataSentiment;
		File inputWorkbook = new File(inputFile);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);
			// Loop over first 10 column and lines
			for (int i = 0; i < sheet.getRows(); i++) {

				dataSentiment = new String[2];
				Cell celldata = sheet.getCell(column, i);
				Cell sentiment = sheet.getCell(readcolumn, i);
				//if(Integer.parseInt(sentiment.getContents().replace(" ", "").replace("\n", "")) == 0){
				dataSentiment[0] = celldata.getContents();
				dataSentiment[1] = sentiment.getContents();
				columnData.put(i, dataSentiment);
				//}

			}

		} catch (BiffException e) {
			e.printStackTrace();
		}
		return columnData;
	}
	
	public static HashMap<Integer, String[]> readFile(File inputWorkbook, int column,
			int readcolumn) throws IOException {
		HashMap<Integer, String[]> columnData = new HashMap<Integer, String[]>();
		String[] dataSentiment;
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);
			// Loop over first 10 column and lines
			for (int i = 0; i < sheet.getRows(); i++) {

				dataSentiment = new String[2];
				Cell celldata = sheet.getCell(column, i);
				Cell sentiment = sheet.getCell(readcolumn, i);
				//if(Integer.parseInt(sentiment.getContents().replace(" ", "").replace("\n", "")) == 0){
				dataSentiment[0] = celldata.getContents();
				dataSentiment[1] = sentiment.getContents();
				columnData.put(i, dataSentiment);
				//}

			}

		} catch (BiffException e) {
			e.printStackTrace();
		}
		return columnData;
	}
	public static HashMap<Integer, String[]> readFiveColumns(File inputFile, int column1,
			int column2,int column3,int column4,int column5) throws IOException, BiffException {
		HashMap<Integer, String[]> columnData = new HashMap<Integer, String[]>();
		String[] dataColumns;
		Workbook w;
		w = Workbook.getWorkbook(inputFile);
		// Get the first sheet
		Sheet sheet = w.getSheet(0);
		// Loop over first 10 column and lines
		for (int i = 0; i < sheet.getRows(); i++) {

			dataColumns = new String[5];
			Cell dataColumn1 = sheet.getCell(column1, i);
			Cell dataColumn2 = sheet.getCell(column2, i);
			Cell dataColumn3 = sheet.getCell(column3, i);
			Cell dataColumn4 = sheet.getCell(column4, i);
			Cell dataColumn5 = sheet.getCell(column5, i);
			
			dataColumns[0] = dataColumn1.getContents();
			dataColumns[1] = dataColumn2.getContents();
			dataColumns[2] = dataColumn3.getContents();
			dataColumns[3] = dataColumn4.getContents();
			dataColumns[4] = dataColumn5.getContents();

			
			columnData.put(i, dataColumns);
			//}
		}

		
		return columnData;	
	}

	public static HashMap<String, Double> readCompareMaps(String filename)
			throws IOException {
		HashMap<String, Double> compareMap = new HashMap<String, Double>();
		File inputWorkbook = new File(filename);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			Sheet sheet = w.getSheet(0);
			for (int i = 0; i < sheet.getRows(); i++) {
				compareMap.put(sheet.getCell(0, i).getContents(),
						Double.parseDouble(sheet.getCell(1, i).getContents()));

			}
		} catch (BiffException e) {
			e.printStackTrace();
		}
		return compareMap;
	}

	public static HashMap<String, Double> compareshizzlefrizzlemizzle(
			String input1, String input2) throws IOException {
		HashMap<String, Double> letssee = new HashMap<String, Double>();
		File inputWorkbookall = new File("CorrectedCounterTrainSetAll.xls");
		File inputWorkbook = new File(input1);
		File inputWorkbook2 = new File(input2);
		Workbook wall;
		Workbook w;
		Workbook w2;
		try {
			wall = Workbook.getWorkbook(inputWorkbookall);
			w = Workbook.getWorkbook(inputWorkbook);
			w2 = Workbook.getWorkbook(inputWorkbook2);
			// Get the first sheet
			Sheet sheetall = wall.getSheet(0);
			Sheet sheet = w.getSheet(0);
			Sheet sheet2 = w2.getSheet(0);

			int total1 = gettotalcount(sheet);
			int total2 = gettotalcount(sheet2);
			// Loop over first 10 column and lines
			for (int i = 1; i < sheetall.getRows(); i++) {
				String word = sheetall.getCell(0, i).getContents();
				double count = getcount(word, sheet);
				double count2 = getcount(word, sheet2);
				count = count / total1;
				count2 = count2 / total2;
				double diff = count - count2;
				if (diff > 0)
					diff = diff / count;
				else if (count2 > 0)
					diff = diff / count2;
				letssee.put(word, diff);
			}

		} catch (BiffException e) {
			e.printStackTrace();
		}
		return letssee;

	}

	public static Double getcount(String word, Sheet sheet) {
		int count = 0;
		for (int i = 0; i < sheet.getRows(); i++) {
			if (word.equals(sheet.getCell(0, i).getContents())) {
				return Double.parseDouble(sheet.getCell(1, i).getContents());
			}
		}
		return 0.0;
	}

	public static int gettotalcount(Sheet sheet) {
		int count = 0;
		for (int i = 0; i < sheet.getRows(); i++) {
			count += Integer.parseInt(sheet.getCell(1, i).getContents());
		}
		return count;

	}
	
	public static HashMap<String, Float> readCounters(String inputFile, int column,
			int readcolumn) throws IOException {
		HashMap<String, Float> columnData = new HashMap<String, Float>();
		File inputWorkbook = new File(inputFile);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);
			// Get the first sheet
			Sheet sheet = w.getSheet(0);
			// Loop over first 10 column and lines
			for (int i = 0; i < sheet.getRows(); i++) {

				Cell celldata = sheet.getCell(column, i);
				Cell sentiment = sheet.getCell(readcolumn, i);
				//if(Integer.parseInt(sentiment.getContents().replace(" ", "").replace("\n", "")) == 0){
				String word = celldata.getContents();
				float count = Float.parseFloat(sentiment.getContents());
				columnData.put(word, count);
				//}

			}

		} catch (BiffException e) {
			e.printStackTrace();
		}
		return columnData;
	}

}
